package com.aispeech.testcustom.services;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import com.aispeech.aios.sdk.AIOSForCarSDK;
import com.aispeech.aios.sdk.listener.AIOSReadyListener;
import com.aispeech.aios.sdk.manager.AIOSCustomizeManager;

import com.aispeech.testcustom.TiApplication;
import com.aispeech.testcustom.i.TiConst;
import com.aispeech.testcustom.i.CustomResultCallBack;
import com.aispeech.testcustom.listener.CustomizeListener;

public class AiosForCarService extends Service {
    private Message   msg;
    private Messenger messenger;

    public AiosForCarService() {
    }

    @Override
    public void onCreate() {}

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("test", "onStartCommand: ");
        Bundle extras = intent.getExtras();

        if (null != extras){
            messenger = (Messenger) extras.get(TiConst.CALL_BACK_RESULT);

        }
        //初始化aios sdk
        AIOSForCarSDK.initialize(TiApplication.getApplication(), new AIOSReadyListener() {
            @Override
            public void onAIOSReady() {
                Log.d("test", "onAIOSReady: in service");
                Log.d("test", "onAIOSReady: init in service");
                //把当前TiApplication.mlist转成regCommands需要格式
//                AIOSCustomizeManager.getInstance().regCommands(TiApplication.convertList4Register());
                AIOSCustomizeManager.getInstance().registerCustomizeListener(new
                        CustomizeListener(new CustomResultCallBack() {

                    @Override
                    public void setResultBack(String s) {
                        msg = Message.obtain();
                        if (null != msg){

                            msg.what = TiConst.MSG_REC_RESULT;
                            Log.d("test", "setResultBack: what is s " + s);
                            msg.obj = s;
                            try {
                                messenger.send(msg);
                            } catch (RemoteException e) {
                                e.printStackTrace();
                                Log.d("test", "setResultBack: 回调失败");
                            }
                        }
                    }
                }));
            }

//            @Override
//            public void onAIOSRebooted() {
//                Log.d("test", "onAIOSRebooted : get daemon reboot in service");
//                onAIOSReady();
//            }
        });

        Toast.makeText(TiApplication.getContext(),"服务中注册完毕",Toast.LENGTH_SHORT).show();

        return Service.START_REDELIVER_INTENT;//确保服务挂了再传intent
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");


    }
}
